<template>
  <div class="common-layout">
    <el-container>
      <el-header class="header">
        <div class="header-container">
          <a aria-current="page" href="#/" title="Fantastic-admin 基础版"
            class="router-link-active router-link-exact-active h-[var(--g-sidebar-logo-height)] w-inherit flex-center gap-2 px-3 text-inherit no-underline cursor-pointer title">
            <img src="../assets/images/logo.png" class="logo h-[30px] w-[30px] object-contain">
            <span class="block truncate font-bold">Fantastic-admin 测试版</span>
          </a>
          <div class="flex items-center">
            <div class="v-popper v-popper--theme-menu v-popper--theme-dropdown flex-center cursor-pointer px-2">
              <div class="flex-center gap-1">
                <img src="https://fantastic-admin.hurui.me/logo.png" alt="" class="h-[24px] w-[24px] rounded-full">
                <el-dropdown>
                  <span class="el-dropdown-link">
                    {{ admin }}
                    <el-icon class="el-icon--right">
                      <arrow-down />
                    </el-icon>
                  </span>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item @click="loginOut">退出</el-dropdown-item>
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
              </div>
            </div>
          </div>
        </div>
      </el-header>
      <el-container>
        <el-aside width="300px">
          <el-row class="tac">
            <el-col>
              <h5 class="mb-2">导航栏</h5>
              <el-menu default-active="3" class="el-menu-vertical-demo">
                <template v-for="(item, index) in menuList" :key="index">
                  <template v-if="item.children">
                    <el-sub-menu :index="item.index">
                      <template #title>
                        <span>{{ item.name }}</span>
                      </template>
                      <template v-for="(itemson, indexson) in item.children" :key="indexson">
                        <template v-if="itemson.children">
                          <el-sub-menu :index="itemson.index">
                            <template #title>
                              <span>{{ itemson.name }}</span>
                            </template>
                            <template v-for="(itemgrandson, indexgrandson) in itemson.children" :key="indexgrandson">
                              <el-menu-item :index="itemgrandson.index" @click="navigate(itemgrandson.component)">
                                <span>{{ itemgrandson.name }}</span>
                              </el-menu-item>
                            </template>
                          </el-sub-menu>
                        </template>
                        <template v-else>
                          <el-menu-item :index="itemson.index" @click="navigate(itemson.component)">
                            <span>{{ itemson.name }}</span>
                          </el-menu-item>
                        </template>
                      </template>
                    </el-sub-menu>
                  </template>
                  <template v-else>
                    <el-menu-item :index="item.index" @click="navigate(item.component)">
                      <span>{{ item.name }}</span>
                    </el-menu-item>
                  </template>
                </template>
              </el-menu>
            </el-col>
          </el-row>
        </el-aside>
        <el-main>
          <Transition name="fade" mode="out-in">
            <keep-alive>
              <component :is="currentTabComponent" class="tab"></component>
            </keep-alive>
          </Transition>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import menuList from '../router/menu';
import RecursiveMenu from '../components/RecursiveMenu/index.vue';
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '../store/userStore'
const currentTabComponent = ref(menuList[1].component);
const router = useRouter();
const userStore = useUserStore()
const navigate = (componentName) => {
  currentTabComponent.value = componentName;
}
const admin = localStorage.getItem('login_account')
const isCollapse = ref(true)

const loginOut = async () => {
  try {
    await userStore.logout()
    router.replace('/login')
  } catch (error) {
    console.error('登出失败：', error)
    ElMessage.error('登出失败，请重试')
  }
}

</script>


<style scoped lang="scss">
.header {
  position: fixed; // 固定头部
  top: 0;
  right: 0;
  left: 0;
  z-index: 2000;
  height: 60px; // 明确指定高度
  padding: 0 24px;
  background-color: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);

  .header-container {
    max-width: 1400px; // 限制最大宽度
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 100%;

    .title {
      display: flex;
      align-items: center;
      gap: 12px;

      span {
        font-size: 18px;
        font-weight: 600;
        color: #1a1a1a;
      }

      .logo {
        height: 36px;
        width: auto;
      }
    }
  }
}

// 侧边栏样式优化
.el-aside {
  background-color: #f5f7fa;
  border-right: 1px solid #e6e6e6;

  .el-menu {
    border-right: none;

    .el-menu-item {
      height: 50px;
      line-height: 50px;

      &:hover {
        background-color: #ecf5ff;
      }

      &.is-active {
        background-color: #e6f2ff;
        color: #409eff;
      }
    }
  }
}

// 主要内容区域
.el-main {
  background-color: #f6f8fa;
  padding: 24px;
  min-height: calc(100vh - 60px);
  margin-top: 60px;
}

// 用户下拉菜单样式
.el-dropdown {
  .el-dropdown-link {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    color: #606266;
    cursor: pointer;

    &:hover {
      color: #409eff;
    }
  }
}

// 清理冗余的工具类
.flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rounded-full {
  border-radius: 9999px;
}

.w-\[24px\] {
  width: 30px;
}

.h-\[24px\] {
  height: 30px;
}

// 添加过渡动画样式
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>